package com.coocaa.framework.ui.dialog.toast;

import android.content.Context;
import android.os.RemoteException;
import android.view.WindowManager;

import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litedaemon.LiteDaemon;
import com.coocaa.x.service.litepm.aidl.ILitePMSpeedUpListener;
import com.tianci.appstore.ui.dialog.SpeedUpDialog;

/**
 * Created by luwei on 15-6-9.
 */
public class ToastDaemon extends LiteDaemon {

    private static LiteDaemonInfo info = null;
    private Context mContext = null;
    private SpeedUpDialog mSpeedUpDialog = null;

    static {
        info = new LiteDaemonInfo();
        info.id = "com.coocaa.framework.ui.dialog.toast.ToastDaemon";
        info.name = "ToastDaemon";
    }

    public ToastDaemon() {
        super(info);
    }

    @Override
    protected void onCreate(Context context) {
        this.mContext = context;
        new Thread("ToastUIStub InitThread") {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (true) {
                    try {
                        Thread.sleep(1);
                        XServiceManager.getLitePMService().addSpeedUpListener(speedUpListener);
                        break;
                    } catch (RemoteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    private ILitePMSpeedUpListener.Stub speedUpListener = new ILitePMSpeedUpListener.Stub() {
        @Override
        public void onSpeedUpStart(int configdelay, final long free, final long total)
                throws RemoteException {
            LogUtils.d("PPPP", "onSpeedUpStart");
            postOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mSpeedUpDialog = new SpeedUpDialog(mContext);
                    mSpeedUpDialog.getWindow()
                            .setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    mSpeedUpDialog.initStatus();
                    mSpeedUpDialog.setInitUsedMemory((int) (100 - (double) free / (double) total
                            * 100.0f));
                    mSpeedUpDialog.show();
                }
            });

        }

        @Override
        public void onSpeedUpReallyKillEnd(final long free, final long total)
                throws RemoteException {
            // TODO Auto-generated method stub
            LogUtils.d("PPPP", "onSpeedUpReallyKillEnd");
            postOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (mSpeedUpDialog != null) {
                        mSpeedUpDialog.setSpeedUsedMemory((int) (100 - (double) free
                                / (double) total * 100.0f));
                    }
                }
            });
        }

        @Override
        public void onSpeedEnd() throws RemoteException {
            // TODO Auto-generated method stub
            LogUtils.d("PPPP", "onSpeedEnd");
            postOnUiThread(new Runnable() {
                public void run() {

                }
            });

        }

    };

    @Override
    protected void onDestroy() {
        try {
            XServiceManager.getLitePMService().removeSpeedUpListener(speedUpListener);
        } catch (RemoteException e) {

        }

    }
}
